package com.hkfn.rfb.common.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.adapter.BaseRecyclerAdapter;
import com.hkfn.rfb.common.adapter.ChooseListAdapter;
import com.hkfn.rfb.common.adapter.MultipleChooseListAdapter;
import com.hkfn.rfb.common.bean.BannerModel;
import com.hkfn.rfb.rfb.bean.RfbDataModel;
import com.hkfn.rfb.rfb.bean.RfbSystemSettingModel;

import java.util.List;

public class DialogUtils {

    /**
     * 创建确定对话框
     * @param context
     * @param msg
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createSureAndCancelDialog(Context context, String msg,
                                                   View.OnClickListener cancelClickListener,
                                                   View.OnClickListener sureClickListener) {
        return createSureAndCancelDialog(context, msg, "取消", "确认", cancelClickListener, sureClickListener);
    }

    /**
     * 创建确定对话框
     * @param context
     * @param msg
     * @param cancelStr
     * @param sureStr
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createSureAndCancelDialog(Context context, String msg, String cancelStr,
                                                   String sureStr, View.OnClickListener cancelClickListener,
                                                   View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_sure_no, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ScreenUtils.getScreenWidth(context) * 3 / 4,
                ViewGroup.LayoutParams.MATCH_PARENT));

        v.findViewById(R.id.tv_title).setVisibility(View.GONE);

        TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);

        tv_msg.setText(msg);
        tv_cancel.setText(cancelStr);
        tv_sure.setText(sureStr);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * 创建确定对话框
     * @param context
     * @param msg
     * @param cancelStr
     * @param sureStr
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createSureAndCancelDialog(Context context, String title, String msg, String cancelStr,
                                                   String sureStr, View.OnClickListener cancelClickListener,
                                                   View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_sure_no, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        TextView tv_title = v.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(title);

        TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);

        tv_msg.setText(msg);
        tv_cancel.setText(cancelStr);
        tv_sure.setText(sureStr);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * 创建时间选择对话框
     * @param context
     * @param textView
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static Dialog createTimePickerDialog(Context context, TextView textView, String otherTime, boolean isStart) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_timepicker, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ScreenUtils.getScreenWidth(context) * 3 / 4,
                ViewGroup.LayoutParams.MATCH_PARENT));

        TimePicker timePicker = v.findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);
        String time = textView.getText().toString();
        if (!StringUtils.isEmpty(time) && !":".equals(time)) {
            timePicker.setHour(Integer.parseInt(time.split(":")[0]));
            timePicker.setMinute(Integer.parseInt(time.split(":")[1]));
        }

        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtils.isEmpty(otherTime) && !":".equals(otherTime)){
                    int otherHour = Integer.parseInt(otherTime.split(":")[0]);
                    int otherMinute = Integer.parseInt(otherTime.split(":")[1]);
                    if (isStart){
                        if (otherHour * 60 + otherMinute - timePicker.getHour() * 60 - timePicker.getMinute() < 10){
                            ToastUtils.showToast(context, "结束时间应大于起始时间10分钟");
                            return;
                        }
                    }else {
                        if (timePicker.getHour() * 60 + timePicker.getMinute() - otherHour * 60 - otherMinute < 10){
                            ToastUtils.showToast(context, "结束时间应大于起始时间10分钟");
                            return;
                        }
                    }
                }
                DialogUtils.closeDialog(dialog);
                String hour = timePicker.getHour() > 10 ? "" + timePicker.getHour() : "0" + timePicker.getHour();
                String minute = timePicker.getMinute() > 10 ? "" + timePicker.getMinute() : "0" + timePicker.getMinute();
                textView.setText(hour + ":" + minute);
            }
        });

        return dialog;
    }

    /**
     * 创建日期选择对话框
     * @param context
     * @param textView
     * @return
     */
    public static Dialog createDatePickerDialog(Context context, TextView textView, long minDate, long maxDate) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_datepicker, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ScreenUtils.getScreenWidth(context) * 3 / 4,
                ViewGroup.LayoutParams.MATCH_PARENT));

        DatePicker datePicker = v.findViewById(R.id.datePicker);
        datePicker.setMinDate(minDate);
        datePicker.setMaxDate(maxDate);

        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
                String year = datePicker.getYear() > 10 ? "" + datePicker.getYear() : "0" + datePicker.getYear();
                String month = (datePicker.getMonth() + 1) > 10 ? "" +  (datePicker.getMonth() + 1) : "0" +  (datePicker.getMonth() + 1);
                String day = datePicker.getDayOfMonth() > 10 ? "" + datePicker.getDayOfMonth() : "0" + datePicker.getDayOfMonth();
                textView.setText(year + "-" + month + "-" + day);
            }
        });

        return dialog;
    }

    /**
     * 创建确定对话框
     * @param context
     * @param rfbSystemSettingModel
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createChangeParamSettingDialog(Context context, RfbSystemSettingModel rfbSystemSettingModel,
                                                        TextWatcher upTextWatcher, TextWatcher downTextWatcher,
                                                        View.OnClickListener cancelClickListener,
                                                        View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_change_param_setting, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        TextView tv_title = v.findViewById(R.id.tv_title);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText(rfbSystemSettingModel.getValueName());

        EditText et_up_value = (EditText) v.findViewById(R.id.et_up_value);
        TextView tv_up_unit = (TextView) v.findViewById(R.id.tv_up_unit);
        EditText et_down_value = (EditText) v.findViewById(R.id.et_down_value);
        TextView tv_down_unit = (TextView) v.findViewById(R.id.tv_down_unit);

        et_up_value.setText(rfbSystemSettingModel.getMaxValue());
        tv_up_unit.setText(rfbSystemSettingModel.getUnit());
        et_down_value.setText(rfbSystemSettingModel.getMinValue());
        tv_down_unit.setText(rfbSystemSettingModel.getUnit());

        et_up_value.addTextChangedListener(upTextWatcher);
        et_down_value.addTextChangedListener(downTextWatcher);

        TextView tv_sure = (TextView) v.findViewById(R.id.tv_sure);
        TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * 创建权限对话框
     * @param context
     * @param msg
     * @param title
     * @param cancelStr
     * @param sureStr
     * @param cancelClickListener
     * @param sureClickListener
     * @return
     */
    public static Dialog createPermissionTip(Context context, String msg, String title, String cancelStr,
                                             String sureStr, View.OnClickListener cancelClickListener,
                                             View.OnClickListener sureClickListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_permission_tip, null);
        Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        TextView tv_msg = v.findViewById(R.id.tv_msg);
        TextView tv_cancel = v.findViewById(R.id.tv_cancel);
        TextView tv_sure = v.findViewById(R.id.tv_sure);
        TextView tv_title = v.findViewById(R.id.tv_title);

        tv_msg.setText(msg);
        tv_cancel.setText(cancelStr);
        tv_sure.setText(sureStr);
        tv_title.setText(title);
        tv_title.setVisibility(View.VISIBLE);
        tv_cancel.setOnClickListener(cancelClickListener);
        tv_sure.setOnClickListener(sureClickListener);

        return dialog;
    }

    /**
     * 选择列表
     * @param context
     * @param list
     * @param title
     * @param position
     * @param onItemClickListener
     * @return
     */
    public static Dialog createChooseListDialog(Context context, List<BannerModel> list, String title, int position,
                                                BaseRecyclerAdapter.OnItemClickListener onItemClickListener){
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_list, null);
        TextView tv_title = view.findViewById(R.id.tv_title);
        View v_line = view.findViewById(R.id.v_line);

        RecyclerView rlv_list = view.findViewById(R.id.rlv_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_list.setLayoutManager(linearLayoutManager);
        ChooseListAdapter chooseListAdapter = new ChooseListAdapter(context);
        chooseListAdapter.setCurrentPosition(position);
        chooseListAdapter.addDatas(list);
        rlv_list.setAdapter(chooseListAdapter);
        chooseListAdapter.setOnItemClickListener(onItemClickListener);

        if (StringUtils.isEmpty(title)){
            tv_title.setVisibility(View.GONE);
            v_line.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlv_list.getLayoutParams();
            layoutParams.topMargin = Utils.dip2px(context, 15);
        }else {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
            v_line.setVisibility(View.VISIBLE);
        }

        final Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getScreenHeight(context) * 2 / 3));
        dialog.setCanceledOnTouchOutside(true);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        return dialog;
    }

    /**
     * 多选列表
     * @param context
     * @param list
     * @param title
     * @return
     */
    public static Dialog createMultipleChooseListDialog(final Context context, final List<BannerModel> list, String title, final MyCallBack myCallBack){

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_multiple_choose_list, null);
        TextView tv_title = view.findViewById(R.id.tv_title);
        View v_line = view.findViewById(R.id.v_line);

        RecyclerView rlv_list = view.findViewById(R.id.rlv_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_list.setLayoutManager(linearLayoutManager);
        final MultipleChooseListAdapter chooseListAdapter = new MultipleChooseListAdapter(context);
        chooseListAdapter.addDatas(list);
        rlv_list.setAdapter(chooseListAdapter);
        chooseListAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<BannerModel>() {
            @Override
            public void onItemClick(View v, int position, BannerModel data) {
                if ("1".equals(data.getActionValue())){
                    data.setActionValue("0");
                }else {
                    int selectSize = 0;
                    for (BannerModel bannerModel : list) {
                        if ("1".equals(bannerModel.getActionValue())){
                            selectSize++;
                        }
                    }
                    if (selectSize >= 5){
                        ToastUtils.showToast(context, "最多选择五个数据");
                    }else {
                        data.setActionValue("1");
                    }
                }
                chooseListAdapter.notifyDataSetChanged();
            }
        });

        if (StringUtils.isEmpty(title)){
            tv_title.setVisibility(View.GONE);
            v_line.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlv_list.getLayoutParams();
            layoutParams.topMargin = Utils.dip2px(context, 15);
        }else {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
            v_line.setVisibility(View.VISIBLE);
        }

        TextView sureView = view.findViewById(R.id.tv_sure);
        sureView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 确认
                myCallBack.multipleSureAction(list);
            }
        });
        TextView cancelView = view.findViewById(R.id.tv_cancel);
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCallBack.multipleCancelAction();
            }
        });

        final Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getScreenHeight(context) * 2 / 3));
        dialog.setCanceledOnTouchOutside(true);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        return dialog;
    }

    public interface MyCallBack {
        void multipleSureAction(List<BannerModel> list);
        void multipleCancelAction();
    }

    /**
     * 改变背景颜色
     */
    public static void darkenBackground(Float bgcolor, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
    }

    public static void closeDialog(Dialog dialog) {
        if (null != dialog && dialog.isShowing()) {
            dialog.cancel();
        }
    }

    public static void showDialog(Dialog dialog) {
        try {
            if (null != dialog && null != dialog.getContext() && null != dialog.getWindow()) {
                dialog.show();
            }
        }catch (Exception e){}
    }

    public static void closePopUpWindow(PopupWindow window) {
        if (null != window && window.isShowing()) {
            window.dismiss();
        }
    }

}
